package cn.kk.api.feign.impl;

import cn.hutool.core.util.NumberUtil;
import cn.kk.api.feign.CommonClient;
import cn.kk.api.feign.UserInfoClient;
import cn.kk.code.ErrorCodeEnum;
import cn.kk.constant.HeaderConstant;
import cn.kk.model.user.entity.UserInfoEntity;
import cn.kk.utils.ExceptionUtil;
import cn.kk.utils.RequestUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;


/**
 * 通用api
 * @author liujunkai
 * @date 2023/8/11 14:57
 */
@Service
public class CommonClientImpl implements CommonClient {

    @Resource
    private UserInfoClient userInfoClient;

    /**
     * 获取当前访问用户
     * @return 获取当前访问用户
     */
    @Override
    public UserInfoEntity currentUser() {
        // 获取用户id
        String uidHeader = RequestUtil.getCurrentRequest().getHeader(HeaderConstant.UID_HEADER);
        // 判空响应登录异常
        ExceptionUtil.isBlank(uidHeader, ErrorCodeEnum.USER_ERROR_A0200);
        ExceptionUtil.assertion(!NumberUtil.isNumber(uidHeader), ErrorCodeEnum.USER_ERROR_A0200);
        UserInfoEntity user = userInfoClient.getUser(Long.valueOf(uidHeader));
        ExceptionUtil.isNull(user, ErrorCodeEnum.USER_ERROR_A0200);
        return user;
    }
}